package com.app.twth.ui.adjust.redamage;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.DialogShowPhotoBinding;
import com.app.twth.databinding.ItemImgurlListBinding;
import com.app.twth.utils.MyDialogUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;


public class ReDamageImgUrlAdapter extends RecyclerView.Adapter<ReDamageImgUrlAdapter.MyViewHolder> {
    private String TAG = getClass().getSimpleName();
    private Context mContext;
    private List<String> images;
    private LayoutInflater mInflater;

    public ReDamageImgUrlAdapter(Context mContext, List<String> images) {
        this.mContext = mContext;
        this.images = images;
        this.mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<String> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemImgurlListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return images == null ? 0 : (images.size() > 6 ? 6 : images.size());
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemImgurlListBinding itemBinding;

        public MyViewHolder(ItemImgurlListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.picture_icon_editor)//图片加载出来前，显示的图片
                    .fallback(R.mipmap.picture_icon_editor) //url为空的时候,显示的图片
                    .error(R.mipmap.picture_icon_warning);//图片加载失败后，显示的图片

            Glide.with(mContext)
                    .load(images.get(position))
                    .apply(options)
                    .into(itemBinding.iv);
            itemBinding.iv.setOnClickListener(v -> {
                showPhotoDialog(images.get(position));
            });
        }
    }

    private AlertDialog dialogPhoto;

    public void showPhotoDialog(String path) {
        Log.e(TAG, "path:" + path);
        dialogPhoto = MyDialogUtil.getDialog(mContext, initPhotoDialogView(path), Gravity.CENTER);
        dialogPhoto.setCanceledOnTouchOutside(true);
        dialogPhoto.show();
    }

    private View initPhotoDialogView(String path) {
        DialogShowPhotoBinding dialogBinding = DialogShowPhotoBinding.inflate(LayoutInflater.from(mContext));
        dialogBinding.ivImg.setOnViewTapListener((v, x, y) -> {
            if (dialogPhoto != null) {
                dialogPhoto.dismiss();
            }
        });
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.picture_icon_editor)//图片加载出来前，显示的图片
                .fallback(R.mipmap.picture_icon_editor) //url为空的时候,显示的图片
                .error(R.mipmap.picture_icon_warning);//图片加载失败后，显示的图片
        Glide.with(mContext).load(path).apply(options).into(dialogBinding.ivImg);
        return dialogBinding.getRoot();
    }
}
